﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Controls the number of augmentation dots that will be added to inserted durational symbols.
    /// </summary>
    public class AugmentationDotsCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="AugmentationDotsCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// A reference to the main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public AugmentationDotsCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Toggle Augmenation Dots";
            ScreenTipText = "Sets the number of augmentation dots that the inserted note or rest will have.";
            DisableReason = "Select a note or rest from the gallery first.";
            mainWindow.DocumentPane.SelectionChanged += new SelectionChangedEventHandler(DocumentPane_SelectionChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return (ApplicationWindow.ActivePage != null && ApplicationWindow.ActivePage.SelectedContent == ViewModel.Elements.SelectedContentType.Music)
                || ApplicationWindow.DocumentPane.SelectedItem is LinearCompositionViewPanel;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            if (ApplicationWindow.DocumentPane.SelectedItem is PagePanel)
            {
                foreach (var section in ApplicationWindow.ActivePage.SelectedSections)
                {
                    if (section.Interior is ViewModel.Elements.CWNotation.MusicSectionInterior)
                    {
                        var symbolInserter = ((ViewModel.Elements.CWNotation.MusicSectionInterior)section.Interior).SymbolInserter;

                        if (ApplicationWindow.AugmentationDot_Button.IsChecked)
                            symbolInserter.AugmentationDots = 1;
                        else if (ApplicationWindow.TwoAugmentationDots_Button.IsChecked)
                            symbolInserter.AugmentationDots = 2;
                        else
                            symbolInserter.AugmentationDots = 0;
                    }
                }
            }
            else
            {
                var section = ((ApplicationWindow.DocumentPane.SelectedItem as LinearCompositionViewPanel).DataContext as ViewModel.Elements.LinearCompositionView).Content;
                if (section.Interior is ViewModel.Elements.CWNotation.MusicSectionInterior)
                {
                    var symbolInserter = ((ViewModel.Elements.CWNotation.MusicSectionInterior)section.Interior).SymbolInserter;

                    if (ApplicationWindow.AugmentationDot_Button.IsChecked)
                        symbolInserter.AugmentationDots = 1;
                    else if (ApplicationWindow.TwoAugmentationDots_Button.IsChecked)
                        symbolInserter.AugmentationDots = 2;
                    else
                        symbolInserter.AugmentationDots = 0;
                }
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the selected document has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void DocumentPane_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (activePage != null)
            {
                activePage.PropertyChanged -= activePage_PropertyChanged;
                activePage = null;
            }

            if (ApplicationWindow.ActivePage != null)
            {
                activePage = ApplicationWindow.ActivePage;
                activePage.PropertyChanged += new PropertyChangedEventHandler(activePage_PropertyChanged);
            }

            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Occurs when a property of the active page has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        void activePage_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedContent")
                RaiseCanExecuteChanged();
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the active page.
        /// </summary>
        private ViewModel.Elements.Page activePage;

        #endregion
    }
}
